//
//  YWSelectMissionCycleTableViewController.m
//  maintain
//
//  Created by Kyo on 16/10/15.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWSelectMissionCycleTableViewController.h"
#import "UIColor+CustomColors.h"
#import "YWMacro.h"

#import "YWDetectAndUpkeepManager.h"

@interface YWSelectMissionCycleTableViewController ()
@property (nonatomic, strong) UIView *footView;
@end

@implementation YWSelectMissionCycleTableViewController
- (instancetype)init{
    return [super  initWithStyle:UITableViewStyleGrouped];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = NSLocalizedString(@"selectMissionCycle", nil);//@"选择任务循环周期";
    self.view.backgroundColor = [UIColor customBackgroudColor];
    
    [self initTableView];
}

- (void)initTableView{
    self.tableView.backgroundColor = [UIColor customBackgroudColor];
    self.tableView.separatorColor = [UIColor customLineColor];
    self.tableView.sectionFooterHeight = 1.0;
    self.tableView.tableFooterView = self.footView;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 7;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuseIdentifier = @"YWSelectMissionCycleCellReuseIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (cell==nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        cell.textLabel.font = kContentFont;
    }
    NSString *cycle = [YWDetectAndUpkeepManager missionCycleStringWithCycle:indexPath.row];
    cell.textLabel.text = cycle;
    if (self.missionCycle == indexPath.row) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.delegate respondsToSelector:@selector(selectMissionCycle:)]) {
        [self.delegate selectMissionCycle:indexPath.row];
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (UIView *)footView{
    if (!_footView) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100)];
        _footView = view;
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(30, 0, SCREEN_WIDTH-60, 100)];
        label.font = kContentFont;
        label.numberOfLines = 0;
        label.textColor = [UIColor customTextGrayColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = NSLocalizedString(@"cycleTips", nil);
        [_footView addSubview:label];
    }
    return _footView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
